<template>
  <div>
    <div class="consultation">
      <el-card class="box-card">
        <Search :searchType="searchType" @consultSearch="consultSearch"></Search>
        <!--表格信息展示-->
        <el-table ref="multipleTable" :data="consultList" tooltip-effect="dark" style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column type="selection"> </el-table-column>
          <el-table-column type="index" label="序号"> </el-table-column>
          <el-table-column prop="stuNum" label="学号" width="120"></el-table-column>
          <el-table-column prop="userInfo.stuName" label="姓名"></el-table-column>
          <el-table-column prop="userInfo.stuSex" :formatter="parseSex" label="性别" width="50"></el-table-column>
          <el-table-column prop="userInfo.stuAddress" label="校区" width="80"></el-table-column>
          <el-table-column prop="userInfo.stuMajor" label="学院" width="160"></el-table-column>
          <el-table-column prop="userInfo.stuPhone" label="电话" width="120"></el-table-column>
          <el-table-column prop="consultDate" label="咨询周"></el-table-column>
          <el-table-column prop="consultTime" label="咨询时间"></el-table-column>
          <el-table-column prop="consultRoom" label="咨询地点" width="200"></el-table-column>
          <el-table-column prop="adminInfo.adminName" label="咨询老师"></el-table-column>
          <el-table-column prop="consultStatus" :formatter="parseStatus" label="咨询状态">
          </el-table-column>
          <el-table-column prop="consultResultCrisisStatus" :formatter="parseConclusion" label="咨询结论"></el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <el-button v-if="scope.row.consultStatus == 3 || scope.row.consultStatus == 2" type="success" size="mini"
                icon="el-icon-circle-check" @click="examineTest(scope.row.consultId)" circle></el-button>
              <el-popconfirm title="结束咨询？" v-if="scope.row.consultStatus == 1" class="final"
                @confirm="final(scope.row.consultId)">
                <el-button slot="reference" type="success" size="mini" icon="el-icon-circle-check" circle></el-button>
              </el-popconfirm>
              <el-button type="primary" size="mini" icon="el-icon-edit" @click="modify(scope.row.consultId)"
                circle></el-button>
              <el-popconfirm title="确定删除吗？" class="delete" @confirm="deleted(scope.row.consultId)">
                <el-button slot="reference" type="danger" size="mini" icon="el-icon-delete" circle></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <div style="margin-top: 20px">
          <el-button @click="toggleSelection()">取消选择</el-button>
          <el-button @click="deleted(0)">删除</el-button>
        </div>
        <!--分页器-->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
          :page-sizes="[10, 20, 30, 40]" :page-size="limit" layout="total, sizes, prev, pager, next, jumper"
          :total="total" background>
        </el-pagination>
      </el-card>
      <el-dialog title="咨询信息" :visible.sync="outerVisible">
        <el-descriptions class="margin-top" :column="2" border>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-user"></i>
              姓名
            </template>
            {{ consultByone.userInfo?.stuName }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-mobile-phone"></i>
              性别
            </template>
            {{ consultByone.userInfo?.stuSex == 1 ? '男' : '女' }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-office-building"></i>
              学号
            </template>
            {{ consultByone.stuNum }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-location-outline"></i>
              校区
            </template>
            {{ consultByone.userInfo?.stuAddress }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-tickets"></i>
              学院
            </template>
            {{ consultByone.userInfo?.stuMajor }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-office-building"></i>
              电话
            </template>
            {{ consultByone.userInfo?.stuPhone }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-office-building"></i>
              咨询时间
            </template>
            {{ consultByone.consultDate + consultByone.consultTime }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-office-building"></i>
              咨询地点
            </template>
            {{ consultByone.consultRoom }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-office-building"></i>
              咨询老师
            </template>
            {{ consultByone.adminInfo.adminName }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-office-building"></i>
              问卷评分
            </template>
            {{ consultByone.visitInfo?.visitCrisisGrades }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-office-building"></i>
              详细描述
            </template>
            {{ consultByone.visitInfo?.userCrisisText }}
          </el-descriptions-item>
        </el-descriptions>
        <div slot="footer" class="dialog-footer">
          <el-button @click="outerVisible = false">取 消</el-button>
          <el-button v-if="consultByone.consultStatus != 2" type="primary"
            @click="pass(consultByone.consultId)">确认</el-button>
        </div>
      </el-dialog>
      <el-dialog title="咨询信息提交" :visible.sync="modifyVisible" width="30%">
        <el-form ref="form" :model="consultByone" label-width="80px">
          <el-form-item label="咨询结论" prop="consultConclusion">
            <el-select v-model="consultByone.consultResultCrisisStatus" placeholder="请选择咨询结论">
              <el-option label="转介送诊" :value="0"></el-option>
              <el-option label="安排咨询" :value="1"></el-option>
              <el-option label="无需咨询" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="咨询补充">
            <el-input type="textarea" v-model="consultByone.consultConclusion"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="modifyVisible = false">取 消</el-button>
          <el-button type="primary" @click="modifySumbit()">确认</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import Search from "@/components/search";
import { reqGetConsultAll, reqGetConsultTotal, reqGetById, reqDeletes, reqPass,reqSubmitResult} from "@/api/Consultation"
export default {
  data() {
    return {
      searchType: "咨询",
      consultByone: {
        adminInfo:{
          adminName:""
        },
      },
      consultList: [],
      //选中行的编号
      rowsVisitId: [],
      //搜索列表
      SearchList: {
        stuNum: "",
        consultStatus: "",
        consultConclusion: "",
        auditStatus: 1,
        adminInfo:{
          adminName:""
        },
        consultTime: "",
        consultDate: "",
        userInfo: {
          stuName: "",
          stuAddress: "",
          stuMajor: "",
        }
      },
      consultStatusMap: {
        '3': '等待咨询',
        '1': '咨询中',
        '2': '咨询结束'
      },
      consultConclusionMap: {
        '0': '转介送诊',
        '1': '安排咨询',
        '2': '无需咨询'
      },
      page: 1,
      limit: 10,
      total: 0,
      outerVisible: false,
      modifyVisible: false,
      props: { multiple: true },
      options: [{
        value: 1,
        label: '东南',
        children: [{
          value: 2,
          label: '上海',
          children: [
            { value: 3, label: '普陀' },
            { value: 4, label: '黄埔' },
            { value: 5, label: '徐汇' }
          ]
        }, {
          value: 7,
          label: '江苏',
          children: [
            { value: 8, label: '南京' },
            { value: 9, label: '苏州' },
            { value: 10, label: '无锡' }
          ]
        }, {
          value: 12,
          label: '浙江',
          children: [
            { value: 13, label: '杭州' },
            { value: 14, label: '宁波' },
            { value: 15, label: '嘉兴' }
          ]
        }]
      }, {
        value: 17,
        label: '西北',
        children: [{
          value: 18,
          label: '陕西',
          children: [
            { value: 19, label: '西安' },
            { value: 20, label: '延安' }
          ]
        }, {
          value: 21,
          label: '新疆维吾尔族自治区',
          children: [
            { value: '乌鲁木齐', label: '乌鲁木齐' },
            { value: '克拉玛依', label: '克拉玛依' }
          ]
        }]
      }]
    };
  },
  components: {
    Search,
  },
  methods: {
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    //获取所有数据的总数
    async getTotal() {
      let result_Total = await reqGetConsultTotal(this.SearchList);
      this.total = result_Total.data.length;
    },
    /*获取初访信息*/
    async getConsultAll() {
      let result = await reqGetConsultAll((this.page - 1) * this.limit, this.limit, this.SearchList);
      if (result.code == 0) {
        this.consultList = JSON.parse(JSON.stringify(result.data));
      }
      this.getTotal();
    },
    //模糊查询
    async consultSearch(consultSearch,num) {
      this.SearchList = {
        userInfo: {
          stuAddress: consultSearch.userInfo.stuAddress,
          stuMajor: consultSearch.userInfo.stuMajor,
          stuName: consultSearch.userInfo.stuName
        },
        adminInfo:{
          adminName:consultSearch.adminInfo.adminName
        },
        consultConclusion: consultSearch.visitConclusion,
        consultStatus: consultSearch.visitStatus,
        consultTime: consultSearch.visitTime,
        consultDate: consultSearch.visitDate,
        stuNum: consultSearch.stuNum,
      };
      this.SearchList.auditStatus = 1;
      this.getConsultAll();
      if (num == 0) {
        this.$message({
          message: '查询成功',
          type: 'success'
        });
      }
    },
    handleSelectionChange(selection) {
      this.rowsConsultId = selection.map(i => {
        return i.consultId;
      })
    },
    handleSizeChange(val) {
      this.limit = val
      this.getConsultAll();
    },
    handleCurrentChange(val) {
      this.page = val
      this.getConsultAll();
    },
    parseSex(row) {
      return row.userInfo.stuSex == 1 ? '男' : '女'
    },
    parseStatus(row) {
      return this.consultStatusMap[row.consultStatus]
    },
    parseConclusion(row) {
      return this.consultConclusionMap[row.consultResultCrisisStatus]
    },
    /*删除某行的数据*/
    async deleted(consultId) {
      let result;
      if (consultId != 0) {
        result = await reqDeletes([consultId]);
      } else {
        result = await reqDeletes(this.rowsConsultId);
      }
      if (result.code == 0) {
        this.getConsultAll();
        this.$message({
          message: '删除成功',
          type: 'success'
        });
        this.rowsConsultId = [];
      }
    },
    //打开审核界面获取数据
    async examineTest(consultId) {
      this.outerVisible = true;
      let result = await reqGetById(consultId);
      if (result.code == 0) {
        this.consultByone = JSON.parse(JSON.stringify(result.data));
      }
    },
    //打开修改界面
    async modify(consultId) {
      this.modifyVisible = true;
      let result = await reqGetById(consultId);
      if (result.code == 0) {
        this.consultByone = JSON.parse(JSON.stringify(result.data));
      }
    },
    //开始初访
    async pass(consultId) {
      let result = await reqPass(consultId, 1, 1)
      if (result.code == 0) {
        this.getConsultAll();
        this.getTotal();
        this.$message({
          message: '开始初访',
          type: 'success'
        });
        this.outerVisible = false;
      }
    },
    //结束初访
    async final(consultId) {
      let result = await reqPass(consultId, 1, 2)
      if (result.code == 0) {
        this.getConsultAll();
        this.getTotal();
        this.$message({
          message: '结束初访',
          type: 'success'
        });
        this.outerVisible = false;
      }
    },
    //提交修改
    async modifySumbit() {
      let result={
        consultConclusion:this.consultByone.consultConclusion,
        consultResultCrisisStatus:this.consultByone.consultResultCrisisStatus,
        consultId:this.consultByone.consultId
      }
      let submitResult=await reqSubmitResult(result);
      if(submitResult.code==0){
        this.getConsultAll();
        this.$message({
          message:'提交成功',
          type:'success'
        })
        this.modifyVisible=false;
      }
    },
  },
  created() {
    this.getConsultAll()
  }
};
</script>
<style scoped>
.delete {
  margin-left: 10px;
}

.final {
  margin-right: 10px;
}
</style>